package org.harsh.transmission.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bouncycastle.util.Arrays;
import org.harsh.common.Constants;
import org.harsh.common.Util;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CustomKey implements ITransable {
    // 消息类型，对应消息头
    private byte msgType;
    // 数据编号
    private long msgId;
    // 版本号
    private byte version;
    // 16字节
    private byte[] msgMd5;
    private byte[] msgData;

    /**
     * 转换为byte arr
     * @return
     */
    public byte[] buildBytes() {
        ByteBuffer buf = ByteBuffer.allocate(26);
        buf.put(msgType).putLong(msgId).put(version)
                .put(msgMd5);
        return Arrays.concatenate(buf.array(), msgData);
    }

    public void parse(byte msgType, long msgId, byte[] data) {
        this.setMsgType(msgType);
        this.setMsgId(msgId);
        this.setVersion(data[0]);
        this.setMsgMd5(Arrays.copyOfRange(data, 1,17));
        this.setMsgData(Arrays.copyOfRange(data, 17, data.length));
    }
}
